'''
draw a graph of agent using the porter stemming algorithm and eliminating stop 
words.
of accuracy opposed to number of features.
'''

from os import system 
from agent1 import Agent1
from one_agent_accurcy import AgentTester
from text_example import createRealDatasets
from numpy import arange
import matplotlib.pyplot as plt
import numpy as np


def tester(sizes = None):    
    
    '''
    sizes is an array for the different numbers of feature to be tested, of it 
    expected to all integers. 
    '''
    
    datasets = createRealDatasets()
    results = {}
    if not sizes.any(): 
        sizes = arange(start=20,stop = 40, step=20)
    
        
    for name, data in datasets.items():
        
        results[name] = {}
                
        for number_of_features in sizes:
            
            class agent(Agent1):
                pass
            agent.set(number_of_features)
            tester = AgentTester()
            results[name][number_of_features] = tester.run(data, agent, 1, 30, 10)
            
    
    averge={}
    for s in sizes:
        averge[s] = 0.0
        for test in results:
            averge[s] += results[test][s]
        averge[s] /= len(results)
        
    results['averge'] = averge
    
    dirname = str(sizes[0])+'_'+str(sizes[-1])+'_test'
    system('mkdir ./'+dirname)
    
    for name, test in results.items():
        
        xs = sizes
        ys = map(lambda x : test[x], sizes)
        f = plt.figure()
        f.suptitle(name)
        ax = f.add_subplot(111)
    
        ax.set_xlabel('#features')
        ax.set_ylabel('accuracy')
        ax.plot(xs, ys, '--o', ms=10, lw=3, mfc='red')
        
        f.savefig('./'+dirname+'/'+name)
    
tests = [
#arange(start=20, stop = 220, step=20),
#arange(start=10, stop = 110, step=10),
#arange(start=5, stop = 55, step=5),
#arange(start=1, stop = 26, step=1),
arange(start=1, stop = 5, step=1),
]

for test in tests:
    tester(test)